package com.star.star.Activity;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.star.star.R;
import com.star.star.fragment.AidFragment;
import com.star.star.fragment.HomeFragment;
import com.star.star.fragment.MeFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private FrameLayout mBodyLayout;
    public LinearLayout mBottomLayout;

    private View mHome;
    private View mStar;
    private View mUser;
    private TextView tv_home;
    private TextView tv_star;
    private TextView tv_user;
    private ImageView iv_home;
    private ImageView iv_star;
    private ImageView iv_user;
    private TextView tv_back;
    private TextView tv_main_title;
    private RelativeLayout rl_title_bar;
    private AidFragment aidFragment;
    private HomeFragment homeFragment;
    private MeFragment meFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置此界面为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
        initBottomBar();
    }

    //获取界面上的UI控件
    private void init() {
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_main_title = (TextView) findViewById(R.id.tv_main_title);
        tv_main_title.setText("明星应援");
        rl_title_bar = (RelativeLayout) findViewById(R.id.title_bar);
        rl_title_bar.setBackgroundColor(Color.parseColor("#FFC0CB"));
        tv_back.setVisibility(View.GONE);
        mBodyLayout = (FrameLayout) findViewById(R.id.main_body);

    }

    //获取底部导航栏上的控件
    private void initBottomBar() {
        mBottomLayout = (LinearLayout) findViewById(R.id.main_bottom_bar);
        mHome = findViewById(R.id.bottom_bar_home_btn);
        mStar = findViewById(R.id.bottom_bar_star_btn);
        mUser = findViewById(R.id.bottom_bar_user_btn);
        tv_home = (TextView) findViewById(R.id.bottom_bar_text_home);
        tv_star = (TextView) findViewById(R.id.bottom_bar_text_star);
        tv_user = (TextView) findViewById(R.id.bottom_bar_text_user);
        iv_home = (ImageView) findViewById(R.id.bottom_bar_image_home);
        iv_star = (ImageView) findViewById(R.id.bottom_bar_image_star);
        iv_user = (ImageView) findViewById(R.id.bottom_bar_image_user);
        initViewAndWords();
        setSelectedStatus(0);
        click();
    }
    public void click() {
        mHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelectedStatus(0);
            }
        });
        mStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelectedStatus(1);
            }
        });
        mUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelectedStatus(2);
            }
        });
    }
    /**
     * 初始化控件和文字属性
     */
    @SuppressLint("ResourceAsColor")
    private void initViewAndWords() {
        tv_home.setTextColor(Color.parseColor("#000000"));
        tv_star.setTextColor(Color.parseColor("#000000"));
        tv_user.setTextColor(Color.parseColor("#000000"));
        iv_home.setImageResource(R.drawable.home);
        iv_star.setImageResource(R.drawable.star);
        iv_user.setImageResource(R.drawable.user);
    }

    //设置底部按钮的选中状态
    private void setSelectedStatus(int index) {
        initViewAndWords();
        switch (index) {
            case 0:
                mHome.setSelected(true);
                iv_home.setImageResource(R.drawable.home_1);
                tv_home.setTextColor(Color.parseColor("#FFFFFF"));
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_main_title.setText("首页");
                createView(index);
                break;
            case 1:
                mStar.setSelected(true);
                iv_star.setImageResource(R.drawable.star_1);
                tv_star.setTextColor(Color.parseColor("#FFFFFF"));
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_main_title.setText("爱豆守护榜");
                createView(index);
                break;
            case 2:
                mUser.setSelected(true);
                iv_user.setImageResource(R.drawable.user_1);
                tv_user.setTextColor(Color.parseColor("#FFFFFF"));
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_main_title.setText("用户");
                createView(index);
                break;
        }
        createView(index);
    }
    private List<Fragment>  baseFragments = new ArrayList<>();
    private FragmentTransaction obtainFragmentTransaction(int index) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        return ft;
    }
    //选择视图
    private void createView(int viewIndex) {
        // 开启一个Fragment事务
        FragmentTransaction transaction = obtainFragmentTransaction(viewIndex);
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (viewIndex) {
            case 0:
                //首页界面
                if (homeFragment == null) {
                    // 如果HdFragment为空，则创建一个并添加到界面上
                    homeFragment = new HomeFragment();
                    baseFragments.add(homeFragment);
                    transaction.add(R.id.main_body, homeFragment, HomeFragment.class.getName());
                } else {
                    // 如果HdFragment不为空，则直接将它显示出来
                    transaction.show(homeFragment);
                }
                break;
            case 1:
                //应援界面
                if (aidFragment == null) {
                    // 如果HdFragment为空，则创建一个并添加到界面上
                    aidFragment = new AidFragment();
                    baseFragments.add(aidFragment);
                    transaction.add(R.id.main_body, aidFragment, AidFragment.class.getName());
                } else {
                    // 如果HdFragment不为空，则直接将它显示出来
                    transaction.show(aidFragment);
                }
                break;
            case 2:
                //用户界面
                if (meFragment == null) {
                    // 如果HdFragment为空，则创建一个并添加到界面上
                    meFragment = new MeFragment();
                    baseFragments.add(meFragment);
                    transaction.add(R.id.main_body, meFragment, MeFragment.class.getName());
                } else {
                    // 如果HdFragment不为空，则直接将它显示出来
                    transaction.show(meFragment);
                }
                break;
        }
        transaction.commit();
    }
    /**
     * 将所有的Fragment都置为隐藏状态。
     * @param transaction 用于对Fragment执行操作的事务
     */
    private void hideFragments(FragmentTransaction transaction) {

        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (aidFragment != null) {
            transaction.hide(aidFragment);
        }
        if (meFragment != null) {
            transaction.hide(meFragment);
        }

    }
}